//
//  ProductCell.m
//  LearnObjectiveC
//
//  Created by 吴可高 on 16/6/13.
//  Copyright © 2016年 吴可高. All rights reserved.
//

#import "ProductCell.h"

static NSString *reuseIndefiner = @"reuseIndefiner";

@interface ProductCell ()
{
    NSMutableArray * _evaluateStars;
    NSMutableArray * _evaluateImgViews;
}
@property (strong, nonatomic) UILabel * evaluateUserNameLabel;
@property (strong, nonatomic) UILabel * evaluateContentLabel;
@end

@implementation ProductCell

+(ProductCell *)productCellWith:(UITableView*)tableView
{
    ProductCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIndefiner];
    if (!cell) {
        cell = [[ProductCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIndefiner];
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        _evaluateImgViews = [NSMutableArray array];
        _evaluateStars = [NSMutableArray array];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initializeStyle];
    }
    return self;
}
-(void)initializeStyle
{
    _evaluateUserNameLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    _evaluateUserNameLabel.textColor = [UIColor lightGrayColor];
    _evaluateUserNameLabel.text = @"小**ma";
    [_evaluateUserNameLabel sizeToFit];
    _evaluateUserNameLabel.font = [UIFont systemFontOfSize:12.f];
    [self.contentView addSubview:_evaluateUserNameLabel];
    //
    _evaluateContentLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    _evaluateContentLabel.textColor = [UIColor grayColor];
    _evaluateContentLabel.numberOfLines = 0.f;
    _evaluateContentLabel.text = @"产品还不错,大小蛮均匀,味道甜甜的";
    _evaluateContentLabel.font = [UIFont systemFontOfSize:14.f];
    [_evaluateContentLabel sizeToFit];
    [self.contentView addSubview:_evaluateContentLabel];
    //
    for (int idx = 0; idx < 5 ; idx ++ ) {
        UILabel * evaluateStarLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        evaluateStarLabel.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:evaluateStarLabel];
        [_evaluateStars addObject:evaluateStarLabel];
    }
    //
    for (int i = 0; i < 4; i ++)
    {
        UIImageView * evaluateImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
        evaluateImgView.backgroundColor = [UIColor brownColor];
        evaluateImgView.layer.cornerRadius = 4.f;
        evaluateImgView.layer.masksToBounds = YES;
        [self.contentView addSubview:evaluateImgView];
        [_evaluateImgViews addObject:evaluateImgView];
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    // 设置星级frame
    CGFloat startMargin  = 30.f;
    CGFloat startPadding = 8.f;
    [_evaluateStars enumerateObjectsUsingBlock:^(UILabel *startLabel, NSUInteger idx, BOOL * _Nonnull stop)
     {
        [startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentView.mas_top).offset(10.f);
            make.size.mas_equalTo(CGSizeMake(12, 12));
            make.left.mas_equalTo(self.contentView.mas_left).offset(startMargin + idx * (12 + startPadding));
        }];
    }];
    // 设置留言user frame
    [_evaluateUserNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        UILabel * label =_evaluateStars[0];
        make.top.mas_equalTo(label.mas_top).offset(-10);
        make.right.mas_equalTo(self.contentView.mas_right).offset(-10);
        make.size.mas_equalTo(CGSizeMake(_evaluateUserNameLabel.frame.size.width, _evaluateUserNameLabel.frame.size.height));
    }];
    // 设置评论frame
    [_evaluateContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        UILabel * label =_evaluateStars[0];
        make.top.mas_equalTo(label.mas_top).offset(16);
        make.left.mas_equalTo(label.mas_left).offset(0.f);
        make.size.mas_equalTo(CGSizeMake(_evaluateContentLabel.frame.size.width,_evaluateContentLabel.frame.size.height));
    }];
    // 设置评价ImgViews frmae
    CGFloat  kImgMargin  = startMargin;
    CGFloat  kImgPadding = startPadding * 1.5f;
    __block   NSInteger i = 0,j = 0;
    [_evaluateImgViews enumerateObjectsUsingBlock:^(UIImageView * imgView, NSUInteger idx, BOOL * _Nonnull stop)
    {
        i = idx % 3;
        j = idx / 3;
        [imgView mas_makeConstraints:^(MASConstraintMaker *make)
         {
             make.size.mas_equalTo(CGSizeMake(60, 60));
             make.top.mas_equalTo(_evaluateContentLabel.mas_bottom).offset(kImgPadding + (60 + kImgPadding)* j);
             make.left.mas_equalTo(_evaluateContentLabel.mas_left).offset(kImgMargin + (60 + kImgPadding)*i);
        }];
    }];
}
@end
